/**
 * 推荐管理管理初始化
 */
var Recommend = {
    id: "RecommendTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Recommend.initColumn = function () {
    return [[
            {title: '邀请用户名', field: 'inviteUserName', visible: true, align: 'center', valign: 'middle'},
            {title: '被邀请用户名', field: 'targetUserName', visible: true, align: 'center', valign: 'middle'},
            {title: '邀请码', field: 'inviteCode', visible: true, align: 'center', valign: 'middle'},
            {title: '推荐人使用免费时长', field: 'takeCount', visible: true, align: 'center', valign: 'middle', templet: function (item) {
                if(item.takeCount === '') {
                    return 0
                }
                return item.takeCount * 3 + ''
                }},
            {title: '推荐人未使用免费时长', field: 'untakeCount', visible: true, align: 'center', valign: 'middle', templet: function (item) {
                if(item.untakeCount === '') {
                    return 0
                }
                return item.untakeCount * 3 + ''
                }},
            {title: '被邀请人购买vip最后时间', field: 'lastOrderTime', visible: true, align: 'center', valign: 'middle'},
            {title: '被邀请人购买次数', field: 'orderCount', visible: true, align: 'center', valign: 'middle'},
            {title: '被邀请人购买总金额', field: 'totalPay', visible: true, align: 'center', valign: 'middle'},
            {title: '状态', field: 'status',
                visible: true, align: 'center',
                valign: 'middle', templet: function (item){
                    if(item.status === '0') {
                        return "未领取"
                    } else {
                        return "已领取"
                    }
                }},
            {title: '注册时间', field: 'createTime', visible: true, align: 'center', valign: 'middle'},
    ]];
};

/**
 * 检查是否选中
 */
Recommend.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Recommend.seItem = selected[0];
        return true;
    }
};

/**
 * 查询推荐管理列表
 */
Recommend.search = function () {
    var queryData = {};
    queryData['inviteUserName'] = $("#inviteUserName").val();
    queryData['targetUserName'] = $("#targetUserName").val();
    queryData['createTime'] = $("#createTime").val();
    queryData['inviteCode'] = $("#inviteCode").val();

    Recommend.table.reload("tableReload",{
        page: {
            curr: 1 //重新从第1页开始
        },
        where: queryData
    });
};

Recommend.exportExcl = function () {
    var inviteUserName = $("#inviteUserName").val();
    var targetUserName = $("#targetUserName").val();
    var createTime = $("#createTime").val();
    var inviteCode = $("#inviteCode").val();
    var hrefUrl =
        Feng.ctxPath + "/recommend/download/excel" +
        "?inviteUserName="+inviteUserName+"&targetUserName="+targetUserName+"&createTime="+createTime+"&inviteCode="+inviteCode;
    window.location.href = hrefUrl;
};

Recommend.tableList = function(queryData){
    layui.use("table",function () {
        var table = layui.table;

        Recommend.table = table;

        table.render({
            elem: "#table",
            url: Feng.ctxPath +"/recommend/list",
            cellMinWidth: 120,
            where: queryData,
            page: true,
            limit: 20,
            cols: Recommend.initColumn(),
            parseData: function(res){
                return{
                    "code":0,
                    "msg":"获取成功",
                    "count":res.total,
                    "data": res.items
                }
            },
            text: {
                none: '暂无相关数据'
            },
            id: "tableReload"
        });
    });
};

$(function () {
    Recommend.tableList(null)
});
